/**
 * Copyright(C) 2012 Luvina
 * UserManaged.java, 25-05-2012 NguyenVietDung
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.logic.MstGroupLogic;
import net.luvina.manageuser.logic.MstJapanLogic;
import net.luvina.manageuser.logic.TblUserLogic;
import net.luvina.manageuser.util.CommonUtil;
import net.luvina.manageuser.util.Message;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sun.org.apache.bcel.internal.generic.LSTORE;

/**
 * UserManaged: Controller để xử lý cho màn hình ADM003, ADM004, ADM006, ADM006
 *
 * @author NguyenVietDung
 *
 */
@Component("userManaged")
@Scope("request")
public class UserManaged extends UserBaseManaged implements Serializable {
	private static final long serialVersionUID = 1L;
	private Date birthday;
	private Integer birthdayDay;
	private Integer birthdayMonth;
	private Integer birthdayYear;
	private String codeLevel;
	private String email;
	private Date endDate;
	private Integer endDay;
	private Integer endMonth;
	private Integer endYear;
	private String fullName;
	private String fullNameKana;
	private Integer groupId;
	private String groupName;
	private String loginName;
	private List<String> lsErrorMessage;
	private List<SelectItem> lsItemBirthdayDay;
	private List<SelectItem> lsItemBirthdayMonth;
	private List<SelectItem> lsItemBirthdayYear;

	private List<SelectItem> lsItemEndDay;
	private List<SelectItem> lsItemEndMonth;
	private List<SelectItem> lsItemEndYear;

	private List<SelectItem> lsItemGroup;
	private List<SelectItem> lsItemJapan;

	private List<SelectItem> lsItemStartDay;
	private List<SelectItem> lsItemStartMonth;
	private List<SelectItem> lsItemStartYear;
	private String messageSuccess;

	private String nameLevel;
	private String password;
	private String passwordConfirm;
	private Date startDate;
	private Integer startDay;
	private Integer startMonth;
	private Integer startYear;
	private String strBirthday;
	private String strStartDate;
	private String strEndDate;

	private String tel;
	private Integer total;
	private String totalStr;
	private Integer userId;
	@Autowired
	private MstGroupLogic mstGroupLogic;
	@Autowired
	private MstJapanLogic mstJapanLogic;
	@Autowired
	private TblUserLogic tblUserLogic;

	/**
	 *
	 * initPage: Hàm chạy đầu tiên khi vào màn hình ADM003
	 *
	 * @param event
	 *            [Param Description]
	 */
	public final void initPage(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
			this.lsItemGroup = this.mstGroupLogic.getListItemGroup("選択してください");
			this.lsItemJapan = this.mstJapanLogic.getListItemJapan("日本語能力");

			this.lsItemBirthdayYear = CommonUtil.getListSelectItemYear(0);
			this.lsItemBirthdayMonth = CommonUtil.getListSelectItemMonth();
			this.lsItemBirthdayDay = CommonUtil.getListSelectItemDay();

			this.lsItemStartYear = CommonUtil.getListSelectItemYear(0);
			this.lsItemStartMonth = CommonUtil.getListSelectItemMonth();
			this.lsItemStartDay = CommonUtil.getListSelectItemDay();

			this.lsItemEndYear = CommonUtil.getListSelectItemYear(1);
			this.lsItemEndMonth = CommonUtil.getListSelectItemMonth();
			this.lsItemEndDay = CommonUtil.getListSelectItemDay();

			this.setDefaultValue();

		}
	}

	/**
	 *
	 * doRegist: Hiển thị màn hình ADM003
	 *
	 * @return String ADM003 [Chuỗi AMD003 để chuyển sang màn hình ADM003]
	 */
	public String doRegist() {

		return CommonConstant.ADM003;
	}

	/**
	 *
	 * setDefaultValue: Thiết định các giá trị mặc định cho màn hình ADM003
	 */
	private void setDefaultValue() {
		// this.loginName = "dungnv8";
		this.groupId = 1;
		this.fullName = "NguyenVietDung";
		this.fullNameKana = "";
		this.birthdayYear = CommonUtil.getYearNow();
		this.birthdayMonth = CommonUtil.getMonthNow();
		this.birthdayDay = CommonUtil.getDayNow();
		this.startYear = CommonUtil.getYearNow();
		this.startMonth = CommonUtil.getMonthNow();
		this.startDay = CommonUtil.getDayNow();
		this.endYear = CommonUtil.getYearNow() + 1;
		this.endMonth = CommonUtil.getMonthNow();
		this.endDay = CommonUtil.getDayNow();

		this.email = "dungnv38@gmail.com";
		this.tel = "0977-102-484";
		this.total = 0;

	}

	/**
	 *
	 * doConfirm: Hiển thị màn hình ADM004
	 *
	 * @return String ADM004 [Chuỗi ADM004 để chuyển sang màn hình ADM004]
	 * @throws ParseException
	 */
	public String doConfirm() throws ParseException {
		this.validate();
		if (this.lsErrorMessage.size() > 0) {
			for (String str : this.lsErrorMessage) {
				System.out.println("+++++" + str);
			}
			return CommonConstant.ADM003;
		}
		return CommonConstant.ADM004;
	}

	/**
	 *
	 * validate: Thực hiện validate thông tin user cho trường hợp validate bên
	 * ManageBean [Param Description]
	 *
	 * @throws ParseException
	 */
	private void validate() throws ParseException {
		this.lsErrorMessage = new ArrayList<String>();

		// In case add new user
		if (this.userId == null) {
			this.userId = 0;
			// Check for existedEmail or not
			boolean isExistEmail = this.tblUserLogic.checkExistedEmail(userId, email);
			if (isExistEmail) {
				String errMsg = "Da ton tai email";
				this.lsErrorMessage.add(errMsg);
			} else {
				this.lsErrorMessage.add("userId is null/ chua ton tai email");
			}

			// Check for existedLoginName or not
			boolean isExistLoginName = this.tblUserLogic.checkExistedLoginName(this.userId, this.loginName);
			if (isExistLoginName) {
				String errMsg = "Da ton tai ten dang nhap";
				this.lsErrorMessage.add(errMsg);
			} else {
				String errMsg = "CHUA ton tai ten dang nhap";
				this.lsErrorMessage.add(errMsg);
			}
			// Check for exist groupId in MstGroup
			boolean isExistMstGroup = this.mstGroupLogic.existMstGroup(this.groupId);
			if (!isExistMstGroup) {
				String errMsg = "Khong ton tai mstgroup";
				this.lsErrorMessage.add(errMsg);
			} else {
				String errMsg = "Da ton tai mstgroup";
				this.lsErrorMessage.add(errMsg);
			}
			// Check for matchedPassword or not
			if (!this.password.equals(this.passwordConfirm)) {
				String errMsg = "Password does not match";
				this.lsErrorMessage.add(errMsg);
			} else {
				String errMsg = "Password matches";
				this.lsErrorMessage.add(errMsg);
			}
			// Check for exist codeLevel in mstJapan
			boolean isExistMstJapan = this.mstJapanLogic.existMstJapan(this.codeLevel);
			if (this.codeLevel != "") {
				if (!isExistMstJapan) {
					String errMsg = "Khong ton tai mstjapan";
					this.lsErrorMessage.add(errMsg);
				} else {
					String errMsg = "Da ton tai mstjapan";
					this.lsErrorMessage.add(errMsg);
				}
				// Check for endDate > startDate ?
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
				this.startDate = simpleDateFormat.parse(this.startYear + "/" + this.startMonth + "/" + this.startDay);
				this.endDate = simpleDateFormat.parse(this.endYear + "/" + this.endMonth + "/" + this.endDay);
				if (this.endDate.compareTo(this.startDate) < 0) {
					String errMsg = "endDate < startDate";
					this.lsErrorMessage.add(errMsg);
				}
				if ("".equals(this.totalStr)) {
					String errMsg = "Ban phai chon totalStr";
					this.lsErrorMessage.add(errMsg);
				}
			}

		}
		// In case edit user

		// kiem tra email da ton tai hay chua Dao, logic, bean, jsf
	}

	/**
	 *
	 * doDone: Ghi data vào database
	 *
	 * @return String ADM006 [Chuỗi ADM006 để chuyển sang màn hình ADM006]
	 */
	public String doDone() {
		return CommonConstant.ADM006;
	}

	/**
	 *
	 * doBack: Quay lại màn hình ADM003
	 *
	 * @return String ADM003 [Chuỗi ADM003 để chuyển sang màn hình ADM003]
	 */
	public String doBack() {
		return CommonConstant.ADM003;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday
	 *            the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 * @return the birthdayDay
	 */
	public Integer getBirthdayDay() {
		return birthdayDay;
	}

	/**
	 * @param birthdayDay
	 *            the birthdayDay to set
	 */
	public void setBirthdayDay(Integer birthdayDay) {
		this.birthdayDay = birthdayDay;
	}

	/**
	 * @return the birthdayMonth
	 */
	public Integer getBirthdayMonth() {
		return birthdayMonth;
	}

	/**
	 * @param birthdayMonth
	 *            the birthdayMonth to set
	 */
	public void setBirthdayMonth(Integer birthdayMonth) {
		this.birthdayMonth = birthdayMonth;
	}

	/**
	 * @return the birthdayYear
	 */
	public Integer getBirthdayYear() {
		return birthdayYear;
	}

	/**
	 * @param birthdayYear
	 *            the birthdayYear to set
	 */
	public void setBirthdayYear(Integer birthdayYear) {
		this.birthdayYear = birthdayYear;
	}

	/**
	 * @return the codeLevel
	 */
	public String getCodeLevel() {
		return codeLevel;
	}

	/**
	 * @param codeLevel
	 *            the codeLevel to set
	 */
	public void setCodeLevel(String codeLevel) {
		this.codeLevel = codeLevel;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the endDay
	 */
	public Integer getEndDay() {
		return endDay;
	}

	/**
	 * @param endDay
	 *            the endDay to set
	 */
	public void setEndDay(Integer endDay) {
		this.endDay = endDay;
	}

	/**
	 * @return the endMonth
	 */
	public Integer getEndMonth() {
		return endMonth;
	}

	/**
	 * @param endMonth
	 *            the endMonth to set
	 */
	public void setEndMonth(Integer endMonth) {
		this.endMonth = endMonth;
	}

	/**
	 * @return the endYear
	 */
	public Integer getEndYear() {
		return endYear;
	}

	/**
	 * @param endYear
	 *            the endYear to set
	 */
	public void setEndYear(Integer endYear) {
		this.endYear = endYear;
	}

	/**
	 * @return the fullName
	 */
	public String getFullName() {
		return fullName;
	}

	/**
	 * @param fullName
	 *            the fullName to set
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @return the fullNameKana
	 */
	public String getFullNameKana() {
		return fullNameKana;
	}

	/**
	 * @param fullNameKana
	 *            the fullNameKana to set
	 */
	public void setFullNameKana(String fullNameKana) {
		this.fullNameKana = fullNameKana;
	}

	/**
	 * @return the groupId
	 */
	public Integer getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId
	 *            the groupId to set
	 */
	public void setGroupId(Integer groupId) {
		this.groupId = groupId;
	}

	/**
	 * @return the groupName
	 */
	public String getGroupName() {
		return groupName;
	}

	/**
	 * @param groupName
	 *            the groupName to set
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName
	 *            the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return the lsErrorMessage
	 */
	public List<String> getLsErrorMessage() {
		return lsErrorMessage;
	}

	/**
	 * @param lsErrorMessage
	 *            the lsErrorMessage to set
	 */
	public void setLsErrorMessage(List<String> lsErrorMessage) {
		this.lsErrorMessage = lsErrorMessage;
	}

	/**
	 * @return the lsItemBirthdayDay
	 */
	public List<SelectItem> getLsItemBirthdayDay() {
		return lsItemBirthdayDay;
	}

	/**
	 * @param lsItemBirthdayDay
	 *            the lsItemBirthdayDay to set
	 */
	public void setLsItemBirthdayDay(List<SelectItem> lsItemBirthdayDay) {
		this.lsItemBirthdayDay = lsItemBirthdayDay;
	}

	/**
	 * @return the lsItemBirthdayMonth
	 */
	public List<SelectItem> getLsItemBirthdayMonth() {
		return lsItemBirthdayMonth;
	}

	/**
	 * @param lsItemBirthdayMonth
	 *            the lsItemBirthdayMonth to set
	 */
	public void setLsItemBirthdayMonth(List<SelectItem> lsItemBirthdayMonth) {
		this.lsItemBirthdayMonth = lsItemBirthdayMonth;
	}

	/**
	 * @return the lsItemBirthdayYear
	 */
	public List<SelectItem> getLsItemBirthdayYear() {
		return lsItemBirthdayYear;
	}

	/**
	 * @param lsItemBirthdayYear
	 *            the lsItemBirthdayYear to set
	 */
	public void setLsItemBirthdayYear(List<SelectItem> lsItemBirthdayYear) {
		this.lsItemBirthdayYear = lsItemBirthdayYear;
	}

	/**
	 * @return the lsItemEndDay
	 */
	public List<SelectItem> getLsItemEndDay() {
		return lsItemEndDay;
	}

	/**
	 * @param lsItemEndDay
	 *            the lsItemEndDay to set
	 */
	public void setLsItemEndDay(List<SelectItem> lsItemEndDay) {
		this.lsItemEndDay = lsItemEndDay;
	}

	/**
	 * @return the lsItemEndMonth
	 */
	public List<SelectItem> getLsItemEndMonth() {
		return lsItemEndMonth;
	}

	/**
	 * @param lsItemEndMonth
	 *            the lsItemEndMonth to set
	 */
	public void setLsItemEndMonth(List<SelectItem> lsItemEndMonth) {
		this.lsItemEndMonth = lsItemEndMonth;
	}

	/**
	 * @return the lsItemEndYear
	 */
	public List<SelectItem> getLsItemEndYear() {
		return lsItemEndYear;
	}

	/**
	 * @param lsItemEndYear
	 *            the lsItemEndYear to set
	 */
	public void setLsItemEndYear(List<SelectItem> lsItemEndYear) {
		this.lsItemEndYear = lsItemEndYear;
	}

	/**
	 * @return the lsItemGroup
	 */
	public List<SelectItem> getLsItemGroup() {
		return lsItemGroup;
	}

	/**
	 * @param lsItemGroup
	 *            the lsItemGroup to set
	 */
	public void setLsItemGroup(List<SelectItem> lsItemGroup) {
		this.lsItemGroup = lsItemGroup;
	}

	/**
	 * @return the lsItemJapan
	 */
	public List<SelectItem> getLsItemJapan() {
		return lsItemJapan;
	}

	/**
	 * @param lsItemJapan
	 *            the lsItemJapan to set
	 */
	public void setLsItemJapan(List<SelectItem> lsItemJapan) {
		this.lsItemJapan = lsItemJapan;
	}

	/**
	 * @return the lsItemStartDay
	 */
	public List<SelectItem> getLsItemStartDay() {
		return lsItemStartDay;
	}

	/**
	 * @param lsItemStartDay
	 *            the lsItemStartDay to set
	 */
	public void setLsItemStartDay(List<SelectItem> lsItemStartDay) {
		this.lsItemStartDay = lsItemStartDay;
	}

	/**
	 * @return the lsItemStartMonth
	 */
	public List<SelectItem> getLsItemStartMonth() {
		return lsItemStartMonth;
	}

	/**
	 * @param lsItemStartMonth
	 *            the lsItemStartMonth to set
	 */
	public void setLsItemStartMonth(List<SelectItem> lsItemStartMonth) {
		this.lsItemStartMonth = lsItemStartMonth;
	}

	/**
	 * @return the lsItemStartYear
	 */
	public List<SelectItem> getLsItemStartYear() {
		return lsItemStartYear;
	}

	/**
	 * @param lsItemStartYear
	 *            the lsItemStartYear to set
	 */
	public void setLsItemStartYear(List<SelectItem> lsItemStartYear) {
		this.lsItemStartYear = lsItemStartYear;
	}

	/**
	 * @return the messageSuccess
	 */
	public String getMessageSuccess() {
		return messageSuccess;
	}

	/**
	 * @param messageSuccess
	 *            the messageSuccess to set
	 */
	public void setMessageSuccess(String messageSuccess) {
		this.messageSuccess = messageSuccess;
	}

	/**
	 * @return the nameLevel
	 */
	public String getNameLevel() {
		return nameLevel;
	}

	/**
	 * @param nameLevel
	 *            the nameLevel to set
	 */
	public void setNameLevel(String nameLevel) {
		this.nameLevel = nameLevel;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the passwordConfirm
	 */
	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	/**
	 * @param passwordConfirm
	 *            the passwordConfirm to set
	 */
	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the startDay
	 */
	public Integer getStartDay() {
		return startDay;
	}

	/**
	 * @param startDay
	 *            the startDay to set
	 */
	public void setStartDay(Integer startDay) {
		this.startDay = startDay;
	}

	/**
	 * @return the startMonth
	 */
	public Integer getStartMonth() {
		return startMonth;
	}

	/**
	 * @param startMonth
	 *            the startMonth to set
	 */
	public void setStartMonth(Integer startMonth) {
		this.startMonth = startMonth;
	}

	/**
	 * @return the startYear
	 */
	public Integer getStartYear() {
		return startYear;
	}

	/**
	 * @param startYear
	 *            the startYear to set
	 */
	public void setStartYear(Integer startYear) {
		this.startYear = startYear;
	}

	/**
	 * @return the strBirthday
	 */
	public String getStrBirthday() {
		return strBirthday;
	}

	/**
	 * @param strBirthday
	 *            the strBirthday to set
	 */
	public void setStrBirthday(String strBirthday) {
		this.strBirthday = strBirthday;
	}

	/**
	 * @return the strStartDate
	 */
	public String getStrStartDate() {
		return strStartDate;
	}

	/**
	 * @param strStartDate
	 *            the strStartDate to set
	 */
	public void setStrStartDate(String strStartDate) {
		this.strStartDate = strStartDate;
	}

	/**
	 * @return the strEndDate
	 */
	public String getStrEndDate() {
		return strEndDate;
	}

	/**
	 * @param strEndDate
	 *            the strEndDate to set
	 */
	public void setStrEndDate(String strEndDate) {
		this.strEndDate = strEndDate;
	}

	/**
	 * @return the tel
	 */
	public String getTel() {
		return tel;
	}

	/**
	 * @param tel
	 *            the tel to set
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}

	/**
	 * @return the total
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the total to set
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return the totalStr
	 */
	public String getTotalStr() {
		return totalStr;
	}

	/**
	 * @param totalStr
	 *            the totalStr to set
	 */
	public void setTotalStr(String totalStr) {
		this.totalStr = totalStr;
	}

	/**
	 * @return the userId
	 */
	public Integer getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	/**
	 * @return the mstGroupLogic
	 */
	public MstGroupLogic getMstGroupLogic() {
		return mstGroupLogic;
	}

	/**
	 * @param mstGroupLogic
	 *            the mstGroupLogic to set
	 */
	public void setMstGroupLogic(MstGroupLogic mstGroupLogic) {
		this.mstGroupLogic = mstGroupLogic;
	}

	/**
	 * @return the mstJapanLogic
	 */
	public MstJapanLogic getMstJapanLogic() {
		return mstJapanLogic;
	}

	/**
	 * @param mstJapanLogic
	 *            the mstJapanLogic to set
	 */
	public void setMstJapanLogic(MstJapanLogic mstJapanLogic) {
		this.mstJapanLogic = mstJapanLogic;
	}

	/**
	 * @return the tblUserLogic
	 */
	public TblUserLogic getTblUserLogic() {
		return tblUserLogic;
	}

	/**
	 * @param tblUserLogic
	 *            the tblUserLogic to set
	 */
	public void setTblUserLogic(TblUserLogic tblUserLogic) {
		this.tblUserLogic = tblUserLogic;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
